import { NextRequest, NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'
import { CellStyle } from '@/types/sheet'

const prisma = new PrismaClient()

// 更新单元格样式
export async function PUT(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const sheetId = params.id
    const body = await request.json()
    const { row, column, style } = body

    if (typeof row !== 'number' || typeof column !== 'number') {
      return NextResponse.json({ error: 'Row and column must be numbers' }, { status: 400 })
    }

    // 使用 upsert 来更新或创建单元格
    const cell = await prisma.cell.upsert({
      where: {
        sheetId_row_column: {
          sheetId,
          row,
          column
        }
      },
      update: {
        style: style as any // Prisma 的 Json 类型
      },
      create: {
        sheetId,
        row,
        column,
        style: style as any
      }
    })

    return NextResponse.json(cell)
  } catch (error) {
    console.error('Error updating cell style:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
}
